package com.shopnight.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopnight.entity.ChatRoomInferior;
import com.shopnight.entity.Post;
import com.shopnight.entity.PostComment;
import com.shopnight.interceptors.JWTInterceptor;
import com.shopnight.mapper.UserMapper;
import com.shopnight.service.PostCommentService;
import com.shopnight.mapper.PostCommentMapper;
import com.shopnight.socketVo.MessageVo;
import com.shopnight.utils.UserContext;
import com.shopnight.vo.PostCommentVo;
import com.shopnight.vo.PostListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import static org.apache.naming.SelectorContext.prefix;

/**
* @author 13201
* @description 针对表【post_comment】的数据库操作Service实现
* @createDate 2023-05-09 17:17:38
*/
@Service
public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment>
    implements PostCommentService{

    @Autowired
    PostCommentMapper postCommentMapper;
    @Resource(name = "taskExecutor")
    Executor executor;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    UserMapper userMapper;

    private String prefix = "mqtt-topic:";

    @Override
    public Page<PostCommentVo> getPostComment(Page page, Integer id) {
        Page<PostCommentVo> res = postCommentMapper.getPostComment(page, id);

        Collections.reverse(res.getRecords());

        res.getRecords().parallelStream().forEach( messageVo -> {
            if (messageVo.getFatherId() != null ){
                PostComment postComment = postCommentMapper.selectById(messageVo.getFatherId());
                PostCommentVo.fatherMessageVo fatherMessageVo = new PostCommentVo.fatherMessageVo();
                BeanUtils.copyProperties(postComment,fatherMessageVo);
                fatherMessageVo.setUserNick( userMapper.selectById(fatherMessageVo.getUserId()).getUserNick() );
                messageVo.setFatherMessageVo(fatherMessageVo);
            }
        });

        return res;
    }

    @Override
    public Map<String, Object> addPostComment(PostComment postComment) {
        String userId =  UserContext.getEndUserId();
        Date date = new Date();
        postComment.setUserId(Integer.valueOf(userId));
        postComment.setCreateDate(date);
        postCommentMapper.insert(postComment);

        Map<String, Object> res = new HashMap<>();
        res.put("id",postComment.getId());
        res.put("date",date);

        //更新redis缓存数据
        if (! stringRedisTemplate.opsForHash().hasKey(prefix + userId , "wall-post:" + postComment.getPostId().toString()  ) ){
            stringRedisTemplate.opsForHash().put(prefix + userId ,"wall-post:" + postComment.getPostId().toString()  ,"0"  );
        }

        //添加未读消息
        CompletableFuture.runAsync( ()->{
            Set<String> keys = stringRedisTemplate.keys("mqtt-topic*");
            if (keys != null) {
                for (String key : keys) {
                    if(! key.equals("mqtt-topic:" + userId ) ){
                        String hashKey = "wall-post:" + postComment.getPostId().toString() ;
                        if (stringRedisTemplate.opsForHash().hasKey(key, hashKey)) {
                            stringRedisTemplate.opsForHash().increment( key ,hashKey , 1 );
                        }
                    }

                }
            }
        },executor);

        return res;
    }



}




